import axios from "axios";
/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data: any, id?: string, parentId?: string, children?: string) {
  let config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children'
  };

  var childrenListMap: any = {};
  var nodeIds: any = {};
  var tree = [];

  for (let d of data) {
    let parentId = d[config.parentId];
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = [];
    }
    nodeIds[d[config.id]] = d;
    childrenListMap[parentId].push(d);
  }

  for (let d of data) {
    let parentId = d[config.parentId];
    if (nodeIds[parentId] == null) {
      tree.push(d);
    }
  }

  for (let t of tree) {
    adaptToChildrenList(t);
  }

  function adaptToChildrenList(o: { [x: string]: any; }) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]];
    }
    if (o[config.childrenList]) {
      for (let c of o[config.childrenList]) {
        adaptToChildrenList(c);
      }
    }
  }
  return tree;
}
/**
 * 数字转资源大小
 * @param size
 * @returns 
 */
export function sizeTostr(size: number) {
  console.log(size);
  
  var data = "";
  if (size < 0.1 * 1024) { //如果小于0.1KB转化成B  
    data = size.toFixed(2) + "B";
  } else if (size < 0.1 * 1024 * 1024) {//如果小于0.1MB转化成KB  
    data = (size / 1024).toFixed(2) + "KB";
  } else if (size < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB  
    data = (size / (1024 * 1024)).toFixed(2) + "MB";
  } else { //其他转化成GB  
    data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
  }
  var sizestr = data + "";
  var len = sizestr.indexOf("\.");
  var dec = sizestr.substr(len + 1, 2);
  if (dec == "00") {//当小数点后为00时 去掉小数部分  
    return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
  }
  return sizestr;
}
export function downloadFile(url: string, fileName: any, fileSuffix: any) {
  axios({
    url,
    method: 'get',
    headers: {
      Authorization: localStorage.getItem('token')
    },
    responseType: 'blob' // 这一行是关键，拿到blob数据类型的文件
  }).then((res) => {
    console.log(res);
    
    let fileTypeMime = '' // 文件 mime 类型，移动端必传，否则下载不成功；pc端可传可不传
    switch (fileSuffix) { // 获取后缀对应的 mime
      case 'png': fileTypeMime = 'image/png'; break;
      case 'doc': fileTypeMime = 'application/msword'; break;
      case 'docx': fileTypeMime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
      case 'jpg': case 'jpeg': fileTypeMime = 'image/jpeg'; break;
      case 'gif': fileTypeMime = 'image/gif'; break;
      case 'svg': fileTypeMime = 'image/svg+xml'; break;
      case 'tif': case 'tiff': fileTypeMime = 'image/tiff'; break;
      case 'txt': fileTypeMime = 'text/plain'; break;
      case 'ppt': fileTypeMime = 'application/vnd.ms-powerpoint'; break;
      case 'pptx': fileTypeMime = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break;
      case 'xls': fileTypeMime = 'application/vnd.ms-excel'; break;
      case 'xlsx': fileTypeMime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
      case 'zip': fileTypeMime = 'application/zip'; break;
      case '7z': fileTypeMime = 'application/x-7z-compressed'; break;
    }
    let blob = window.URL.createObjectURL(new Blob([res.data], {
      'type': fileTypeMime
    }))
    let link = document.createElement('a')
    link.style.display = 'none'
    link.href = blob
    link.setAttribute('download', fileName + fileSuffix)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link) //下载完成移除元素
    window.URL.revokeObjectURL(blob) //释放掉 blob 对象
  })
}